extends Node

const ARC_POINTS := 8

@onready var area_2d : Area2D = $Area2D
@onready var card_arc : Line2D = $CanvasLayer/CardArc

var current_card : CardUI
var targeting := false

func _ready() -> void:
	Events.card_aim_started.connect(_on_card_aim_started)
	Events.card_aim_ended.connect(_on_card_aom_ended)
	
func _process(delta: float) -> void:
	if not targeting :
		return
	
	area_2d.position = get_local_mouse_position()
	card_arc.points = _get_points()
	
func _get_points() -> Array :
	var points := []
	var start := current_card.global_position
	start.x += (current_card.size.x / 2)
	var target := get_local_mouse_position()
	var distance := (target - start)
	
	for i in range(ARC_POINTS) :
		var t := (1.0 / ARC_POINTS) * i
		var x := start.x + (distance.x / ARC_POINTS) * i
		var y := start.y + ease_out_cubic(t) * distance.y
		points.append(Vector2(x, y))
		
	points.append(target)
	return points
	
func ease_out_cubic(number : float) -> float :
	return 1.0 - pow(1.0 - number, 3)
		
func _on_card_aim_started(card : CardUI) -> void :
	if not card.card.is_single_atargeted() :
		return
		
	targeting = true
	area_2d.monitoring = true
	area_2d.monitorable = true
	current_card = card
	
	
func _on_card_aom_ended(card : CardUI) -> void :
	targeting = false
	card_arc.clear_points()
	area_2d.position = Vector2.ZERO
	area_2d.monitoring = false
	area_2d.monitorable = false
	current_card = null
	
	
		
